package Master.interfaces;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Master.Resource;
import Module.StatusService.WorkerStatus;

/*
 * JDialog extended from DetailDialog for displaying worker details
 */
public class WorkerDetailDialog extends DetailDialog {
	private JLabel resource_id = new JLabel("Resource ID");
	private JLabel hostname = new JLabel("Host name");
	private JLabel address = new JLabel("IP Address");
	private JLabel port = new JLabel("Port");
	private JLabel cost = new JLabel("Cost per sec");
	private JLabel status = new JLabel("Status");

	private JLabel resource_id_data = new JLabel();
	private JLabel hostname_data = new JLabel();
	private JLabel address_data = new JLabel();
	private JLabel port_data = new JLabel();
	private JLabel cost_data = new JLabel();
	private JLabel status_data = new JLabel();
	
	private JButton close_btn = new JButton("Close");
	
	private Resource this_resource;

	public WorkerDetailDialog(String title,Resource this_resource,
			WorkerStatus status) {
		super(title);
		this.this_resource=this_resource;
		this.setSize(220,170);
		
		close_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		
		initialiseGUI();
		loadData(status);
	}

	private void initialiseGUI()
	{
		this.setLayout(new BorderLayout());
		this.add(close_btn,BorderLayout.SOUTH);
		
		JPanel info_p=new JPanel();
		info_p.setLayout(new GridBagLayout());
		this.add(info_p,BorderLayout.WEST);
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;	
		
		c.insets=new Insets(1,5,1,5);
		c.gridx = 0;
		c.gridy = 0;
		info_p.add(resource_id,c);
		
		c.gridx = 0;
		c.gridy = 1;
		info_p.add(hostname,c);
		
		c.gridx = 0;
		c.gridy = 2;
		info_p.add(address,c);
		
		c.gridx = 0;
		c.gridy = 3;
		info_p.add(port,c);
		
		c.gridx = 0;
		c.gridy = 4;
		info_p.add(cost,c);
		
		c.gridx = 0;
		c.gridy = 5;
		info_p.add(status,c);
				
		c.gridx = 1;
		c.gridy = 0;
		info_p.add(resource_id_data,c);
		
		c.gridx = 1;
		c.gridy = 1;
		info_p.add(hostname_data,c);
		
		c.gridx = 1;
		c.gridy = 2;
		info_p.add(address_data,c);
		
		c.gridx = 1;
		c.gridy = 3;
		info_p.add(port_data,c);
		
		c.gridx = 1;
		c.gridy = 4;
		info_p.add(cost_data,c);
		
		c.gridx = 1;
		c.gridy = 5;
		info_p.add(status_data,c);
	}
	
	private void loadData(WorkerStatus status)
	{
		resource_id_data.setText(""+this_resource.getID());
		hostname_data.setText(this_resource.getHostName());
		address_data.setText(this_resource.getAddress());
		port_data.setText(this_resource.getPortNo()+"");
		cost_data.setText(""+this_resource.getCost());
		
		switch(status)
		{
			case dead:
				status_data.setText("Not responding");
				break;
			case active:
				status_data.setText("Idle, waiting for work");
				break;
			case running:
				status_data.setText("Running given job");
				break;
		}
	}
	
	private void close()
	{
		this.dispose();
	}
}
